package cn.favebook.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;

import cn.favebook.Global;
import cn.favebook.bean.User;
import cn.favebook.bean.Weibo;
import cn.favebook.db.WeiboDao;

/**
 * Servlet implementation class AddWeiboServlet
 */
@WebServlet("/zone/add.do")
public class AddWeiboServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String content = request.getParameter("content");
		User user = (User) request.getSession().getAttribute(Global.CURRENT_USER);
		QueryRunner runner = (QueryRunner) request.getServletContext().getAttribute(Global.KEY_QUERY_RUNNER);
		Weibo weibo = new Weibo();
		weibo.setContent(content);
		weibo.setPublishDate(new Date());
		weibo.setUserName(user.getUsername());
		try {
			weibo.setUserID(user.getId());
		} catch (Exception e) {
			response.sendRedirect("signin.jsp");
		}
		
		
		
		try {
			WeiboDao.add(runner, weibo);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new IOException();
		}
		
		response.sendRedirect("/favebook/mineindex.jsp");
	}

}
